package com.test.qnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.activity.DetailsActivity;
import com.test.qnews.adapter.CommonAdapter;
import com.test.qnews.adapter.VideoAdapter;
import com.test.qnews.beans.GsonImgBean;
import com.test.qnews.beans.GsonVideoBean;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.beans.VideoBeans;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class VideoFragment extends Fragment{

    private Context context;
    private VideoAdapter videoAdapter;
    private ListView listView;
    private List<VideoBeans> data;
    private String url = "http://c.3g.163.com/nc/video/home/0-10.html";




    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_recommend);
        data();
    }

    private void data() {
        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                data = new ArrayList<>();
                GsonVideoBean gsonBean = gson.fromJson(response, GsonVideoBean.class);
                for (int i = 0; i < gsonBean.getVideoList().size(); i++) {
                    VideoBeans videoBeans = new VideoBeans();
                    videoBeans.setTitle(gsonBean.getVideoList().get(i).getTitle());
                    videoBeans.setCover(gsonBean.getVideoList().get(i).getCover());
                    videoBeans.setVideoUrl(gsonBean.getVideoList().get(i).getMp4_url());
                    data.add(videoBeans);


                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        }
                    });
                }

                videoAdapter = new VideoAdapter(data, context);
                listView.setAdapter(videoAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }
}
